﻿using UnityEngine;
using System.Collections;
using System.IO;
using System.Xml;
using UnityEditor;

public class Function
{
	/// <summary>
	/// 删除文件夹
	/// </summary>
	/// <returns>The folder.</returns>
	/// <param name="dir">Dir.</param>
	public static void DeleteFolder (string dir)
	{
		foreach (string d in Directory.GetFileSystemEntries (dir)) {
			if (File.Exists (d)) {
				if (d.Contains ("svn")) {
					return;
				}
				FileInfo fi = new FileInfo (d);
				if (fi.Attributes.ToString ().IndexOf ("ReadOnly") != -1)
					fi.Attributes = FileAttributes.Normal;
				File.Delete (d);
			} else {
				if (d.Contains ("svn")) {
					return;
				}
				DirectoryInfo d1 = new DirectoryInfo (d);
				if (d1.GetFiles ().Length != 0) {
					DeleteFolder (d1.FullName);////递归删除子文件夹
				}
				Directory.Delete (d);
			}
		}
	}

	/// <summary>
	/// 删除文件
	/// </summary>
	/// <returns>The file.</returns>
	/// <param name="fileNameDir">File name dir.</param>
	public static void DeleteFile (string fileNameDir)
	{
		Debug.Log (fileNameDir);
		if (File.Exists (fileNameDir)) {
			Debug.Log ("DeleteFile");
			FileInfo fi = new FileInfo (fileNameDir);
			if (fi.Attributes.ToString ().IndexOf ("ReadOnly") != -1)
				fi.Attributes = FileAttributes.Normal;
			File.Delete (fileNameDir);
		}
	}


	public static void FindAllSVNFolder (string path)
	{
		string[] folders = Directory.GetDirectories (path);  
		foreach (var folder in folders) {  
			FindAllSVNFolder (folder);
			if (folder.EndsWith (".svn")) {
				DirectoryInfo fatherFolder = new DirectoryInfo (folder);
				fatherFolder.Attributes = FileAttributes.Normal;
				Directory.Delete (folder, true);
				Debug.Log (folder);
			}
		}  
	}

	/// <summary>
	/// 拷贝文件夹
	/// </summary>
	/// <returns>The directory.</returns>
	/// <param name="sourcePath">Source path.</param>
	/// <param name="destinationPath">Destination path.</param>
	public static void CopyDirectory (string sourcePath, string destinationPath)
	{
		DirectoryInfo info = new DirectoryInfo (sourcePath);
		Directory.CreateDirectory (destinationPath);
		foreach (FileSystemInfo fsi in info.GetFileSystemInfos ()) {
			string destName = Path.Combine (destinationPath, fsi.Name);
			if (fsi is System.IO.FileInfo) {
				//				if (fsi.FullName.Contains ("svn")) {
				//					return;
				//				}
				File.Copy (fsi.FullName, destName);
			} else {
				//				if (fsi.FullName.Contains ("svn")) {
				//					return;
				//				}
				Directory.CreateDirectory (destName);
				CopyDirectory (fsi.FullName, destName);
			}
		}
	}


	/// <summary>
	/// 重命名文件夹
	/// </summary>
	/// <returns>The directory.</returns>
	/// <param name="sourcePath">Source path.</param>
	/// <param name="newPath">New path.</param>
	public static void RenameDirectory (string sourcePath, string newPath)
	{
		try{
			Directory.Move(sourcePath,newPath);
		}catch (System.Exception e){
			Debug.Log (e.Message);
		}
	}



	/// <summary>
	/// 拷贝文件
	/// </summary>
	/// <returns>The file.</returns>
	/// <param name="sourcePath">Source path.</param>
	/// <param name="destinationPath">Destination path.</param>
	public static void CopyFile (string sourcePath, string destinationPath)
	{
		if (File.Exists (sourcePath)) {
			File.Copy (sourcePath, destinationPath);
		}
	}
		
	/// 更新xml MetaData
	public static void UpdateXMLMetaData (string xmlPath, string searchName, string changeValue)
	{
		if (File.Exists (xmlPath)) {
			XmlDocument xml = new XmlDocument ();
			xml.Load (xmlPath);
			foreach (XmlElement meta_data in xml.GetElementsByTagName ("meta-data")) {
				if (meta_data.GetAttribute ("android:name") == searchName) {
					meta_data.SetAttribute ("android:value", changeValue);
				}
			}
			xml.Save (xmlPath);
		}
	}

	/// <summary>
	/// 读取文件
	/// </summary>
	public static ArrayList LoadFile (string path, string name)
	{
		StreamReader sr = null;
		try {
			sr = File.OpenText (path + "//" + name);
		} catch (System.Exception e) {
			Debug.LogError(e.Message);
			return null;
		}
		string line;
		ArrayList arrlist = new ArrayList ();
		while ((line = sr.ReadLine ()) != null) {
			arrlist.Add (line);
		}
		sr.Close ();
		sr.Dispose ();
		return arrlist;
	}

	/// <summary>
	/// 创建文件
	/// </summary>
	/// <returns>The file.</returns>
	/// <param name="path">Path.</param>
	/// <param name="name">Name.</param>
	/// <param name="info">Info.</param>
	public static void CreateFile (string path, string name, string info)
	{
		//文件流信息
		StreamWriter sw;
		FileInfo t = new FileInfo (path + "//" + name);
		if (!t.Exists) {
			//如果此文件不存在则创建
			sw = t.CreateText ();
		} else {
			//如果此文件存在则打开
			sw = t.AppendText ();
		}
		//以行的形式写入信息
		sw.WriteLine (info);
		//关闭流
		sw.Close ();
		//销毁流
		sw.Dispose ();
	}

	public class HintWindow : EditorWindow
	{
		static string message = "";
		static EditorWindow my;

		public static void Init (string msg)
		{
			message = msg;
			my = EditorWindow.GetWindow (typeof(HintWindow), true, "提示", true);
		}

		void OnGUI ()
		{
			if (my != null) {
				GUI.Label (new Rect (my.position.width / 2 - 50, my.position.height - 120, 120, 40), message);
			}
		}
	}
}

